public class JoueurMinimax implements Joueur {
  private final HTools tools;

  private static final int MAX_PROF = 7; 
  
  public JoueurMinimax(HTools tools) {
    this.tools = tools;
  } 
  
  public int[] tour(int[] t) {
    int[] cp = new int[18];
    System.arraycopy(t, 0, cp, 0, 16);
    int[] max = max(cp, 0, MAX_PROF, t[10], (t[10] + 1) % 2);
    return new int[] {max[1], max[2]}; 
  }
  
  public int[] max(int[] t, int h, int maxProf, int me, int you) {
    int[] r;
    
    int nbrSucc = Apagos.nbrSucc(t);
    
    if (nbrSucc == 0 || h == maxProf) {
      int[] cp = new int[16];
      System.arraycopy(t, 0, cp, 0, 16);
      r = new int[]{tools.h(cp, me, you)};
    } else {
      int[][] succ = Apagos.succ(t);
      int[] max = new int[] {min(succ[0], h + 1, maxProf, me, you)[0], succ[0][16], succ[0][17]};
      int i = 1;
      while (i != succ.length) {
        int[] tmp = new int[] {min(succ[i], h + 1, maxProf, me, you)[0], succ[i][16], succ[i][17]};
        if (tmp[0] > max[0]) {
          max = tmp;
        }
        i++;
      }
      r = max;
    }
    return r;
  }  
  
  public int[] min(int[] t, int h, int maxProf, int me, int you) {
    int[] r;
    
    int nbrSucc = Apagos.nbrSucc(t);
    
    if (nbrSucc == 0 || h == maxProf) {
      int[] cp = new int[16];
      System.arraycopy(t, 0, cp, 0, 16);
      r = new int[]{tools.h(cp, me, you)};
    } else {
      int[][] succ = Apagos.succ(t);
      
      int[] min = new int[] {max(succ[0], h + 1, maxProf, me, you)[0], succ[0][16], succ[0][17]};
      int i = 1;
      while (i != succ.length) {
        int[] tmp = new int[] {max(succ[i], h + 1, maxProf, me, you)[0], succ[i][16], succ[i][17]};
        if (tmp[0] < min[0]) {
          min = tmp;
        }
        i++;
      }
      r = min;
    }
    return r;
  }  
}